package clientapp;


import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class ReturnMovie extends JFrame implements ActionListener
{
	
	private int Count = 0;
	private JComboBox cmbMov = new JComboBox();
	private JLabel lblSelMov = new JLabel("Select movie to return:");
	private JButton btnReturn = new JButton("Return");
	private JButton btnCancel = new JButton("Cancel");
	
	private String movies[];
	private String Movie = new String();
	private String code = new String();
	 
	/*depending on which item the user selects in combobox, eg.
	  array-index index 0, which is the the first combobox item, will
	  store the corresponding index in dvd/customers object array
	 */
	private Client clientr;
	public ReturnMovie(Client clientp)
	{
        clientr = (Client)clientp;
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(400,150);
		setResizable(false);
		setTitle("Return Movie");
		setLocationRelativeTo(null);
		addWindowListener (new WindowAdapter ()
		{
	    	public void windowClosing (WindowEvent e)
	    	{ //overiding windowClosing
				runStore.store.setEnabled(true);
	    	}
		} //the improved WindowListener is added.
		);
		
		clientr.sendQuery("ReturnMovie1");
		//load jcombobox
		
		 	loadMovieCombo();
		
		//layout
		setLayout(new BorderLayout());
		
		JPanel centerPanel = new JPanel(new FlowLayout());
		centerPanel.add(lblSelMov);
		
		centerPanel.add(cmbMov);
		
		JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		bottomPanel.add(btnReturn);
		bottomPanel.add(btnCancel);
		
		btnReturn.addActionListener(this);
		btnCancel.addActionListener(this);
		add(centerPanel,BorderLayout.CENTER);
		add(bottomPanel,BorderLayout.SOUTH);
		add(new JPanel(), BorderLayout.NORTH);
		
		setVisible(true);
	}
	
	private void loadMovieCombo()
	{
		movies = new String[ clientr.getResult().length];
		for (int i = 0; i < clientr.getResult().length; i++)
		{
			for(int j = 0; j < 2; j++)
			{
				code = clientr.getResult()[i][0];
				Movie = clientr.getResult()[i][1];
			}
			String outMovie = code + " " + Movie;
			movies[i] =  outMovie;
		}
		
		cmbMov = new JComboBox(movies);
		
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource()== btnReturn)
		{	
			if (cmbMov.getItemCount() < 1 && !clientr.getResult()[0][0].equals("#No Records"))
			{
				JOptionPane.showMessageDialog(null, "No movies are currently out");
				//runStore.store.setEnabled(true);
				dispose();
			}
			else
			{
				String selectedMovie = (String)cmbMov.getSelectedItem();
				int movieLength = selectedMovie.length()-2;
				code = selectedMovie.substring(0,2);
				String title = selectedMovie.substring(2,movieLength);
				String sendToServer = "ReturnMovie2" + "#" + code ;
				clientr.sendQuery(sendToServer);
				
				cmbMov.removeItem(selectedMovie);
			}	
		}
		
		else if(e.getSource()==btnCancel)
		{
			//runStore.store.setEnabled(true);
			dispose();
		}
	}
}